package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.etc.entity.Travels;
import com.etc.service.CommentService;
import com.etc.service.TravelsService;
import com.etc.service.UserService;
import com.etc.util.Base64Util;
import com.etc.util.FileUtil;
import com.etc.util.HttpUtil;
import com.etc.util.Message;
import com.etc.util.Util;

@Controller
@RequestMapping(value = "traapi")
public class TravelsController {

	@Resource
	private Message msg;

	@Resource
	private TravelsService ts;
	
	@Resource
	private CommentService cs;
	
	@Resource
	private UserService us;

	/**
	 * 获取图片的路径
	 * 
	 * @param request
	 * @param myfile
	 * @return
	 */
	@RequestMapping(value = "url", method = RequestMethod.POST)
	@ResponseBody
	public Message test(HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile myfile) {
		// 支持上传文件的格式
		String suffixList = "jpg,gif,png,ico,bmp,jpeg";
		// 获取文件名
		String filename = myfile.getOriginalFilename();

		// 获取文件后缀
		String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
		// 判断获取到的后缀是否存在于suffixList
		if (suffixList.contains(suffix)) {
			// \转义
			String path = "D:\\apache-tomcat-8.5.13-windows-x64\\apache-tomcat-8.5.13\\webapps\\file\\" + myfile.getOriginalFilename();
			System.out.println(path);
			request.setAttribute("file", path);
			File file = new File(path);
			try {
				FileUtils.copyInputStreamToFile(myfile.getInputStream(), file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 符合格式就跳转到WEB-INF/jsp/success.jsp
			String path2 ="http://192.168.11.242:8080/file/"+ myfile.getOriginalFilename();
			msg.setCode(0);
			msg.setMsg("成功");
			msg.setFilepath(path2);
			msg.setLocalhostpath(path);
			return msg;
		}
		// 不符合格式就跳转到WEB-INF/jsp/error.jsp
		// forward:form.jsp
		msg.setCode(-1);
		msg.setMsg("失败");
		msg.setFilepath("");
		return msg;
	}
	
	/**
	 * 添加游记的方法
	 * @param title
	 * @param desc
	 * @param url
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "travels", method = RequestMethod.POST)
	@ResponseBody
	public Message addTravels(String title, String desc, String url, int userid) {
		Travels tr = new Travels(title, desc, url, new Date(), userid);
		boolean flag = ts.insertSelective(tr);
		msg.setMsg("添加失败");
		if (flag) {
			msg.setMsg("添加成功");
		}
		return msg;
	}
	
	/**
	 * 实名认证
	 * @param request
	 * @param myfile
	 * @return
	 */
	@RequestMapping(value = "shiming", method = RequestMethod.POST)
	@ResponseBody
	public Message shiMing(HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile myfile) {
		// 身份证识别url
				String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
				Message message = test(request, myfile);
				// 本地图片路径
				String filePath = message.getLocalhostpath();
				try {
					byte[] imgData = FileUtil.readFileByBytes(filePath);
					String imgStr = Base64Util.encode(imgData);
					// 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
					String params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
							+ URLEncoder.encode(imgStr, "UTF-8");
					/**
					 * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
					 */
					String accessToken = Util.getAuth();
					String result = HttpUtil.post(idcardIdentificate, accessToken, params);
					message.setMsg(result);
					return message;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
	}
	
	@RequestMapping(value="travels/{traid}",method=RequestMethod.GET)
	public String selectTravels(@PathVariable(value = "traid") int traid,HttpServletRequest request) {
		Travels travels = ts.selectByPrimaryKey(traid);
		request.setAttribute("travelsBytraid", travels);
		int a = cs.selectCountByTraid(traid);
		request.setAttribute("comment", a);
		return "travels";
	}
	
	@RequestMapping(value="travelsall",method=RequestMethod.GET)
	public String selectTravelsAll(HttpServletRequest request) {
		List<Travels> list = ts.selectAll();
		request.setAttribute("travelsAll", list);
		return "travelsall";
	}
	
	@RequestMapping(value="edit/{userid}",method=RequestMethod.GET)
	@ResponseBody
	public Message updateUer(@PathVariable(value = "userid") int userid,HttpServletRequest request) {
		boolean b = us.updateStauts(userid);
		msg.setMsg("身份认证失败");
		if(b) {
			msg.setMsg("身份认证成功");
		}
		return msg;
	}
}
